package com.net;

import com.housekeeperapp.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xiaolong.wei on 2018/8/13.
 */


public class HKHttpUtils {
    public static OkHttpClient mClient;
    public static Retrofit mRetrofit;
    public static Retrofit mMsgRetrofit;

    private static OkHttpClient getOkHttpClient(){

//        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
//        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);



        mClient  = new OkHttpClient.Builder()
                .addInterceptor(new HKInterceptor())
//                .addNetworkInterceptor(logInterceptor)
                .connectTimeout(20, TimeUnit.SECONDS)
                .build();

        return mClient;
    }

    public static Retrofit getRetrofit(){
        if (mClient == null) {
           getOkHttpClient();
        }
        mRetrofit = new Retrofit.Builder()
                .baseUrl(ApiConfig.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(mClient)
                .build();

        return mRetrofit;
    }

    public static Retrofit getMsgRetrofit(){
        if (mClient == null){
            getOkHttpClient();
        }
        mMsgRetrofit = new Retrofit.Builder()
                .baseUrl(ApiConfig.MSG_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(mClient)
                .build();
        return mMsgRetrofit;
    }

    public static ApiService getApiService(){
        if (mRetrofit == null){
            getRetrofit();
        }
        return mRetrofit.create(ApiService.class);
    }

    public static MsgApiService getMsgApiService(){
        if (mMsgRetrofit == null){
            getMsgRetrofit();
        }
        return mMsgRetrofit.create(MsgApiService.class);
    }
}
